package com.muse.app.controller;

import com.muse.app.service.SmsService;
import com.muse.app.service.UserService;
import com.muse.entity.Result;
import com.muse.interceptor.CurrentContext;
import com.muse.model.LoginParam;
import com.muse.model.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * User: lee
 * Date: 2021/4/20
 * Time: 4:22 下午
 * Description:
 */
@RestController
@RequestMapping(value = "/muse/login")
@Slf4j
public class LoginController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private UserService userService;

    @GetMapping("/sendSmsCode")
    public Result sendSmsCode(@RequestParam("phone") String phone, @RequestParam("type") Integer type) {
        log.info("sendSmsCode,phone ={},type={}", phone, type);
        try {
            smsService.sendSmsCode(phone, type);
            return Result.success();
        } catch (Exception e) {
            log.error("sendSmsCode error, phone={}", phone, e);
            return Result.failure();
        }
    }

    @PostMapping("")
    public Result<UserInfoVo> login(@RequestBody LoginParam param) {
        log.info("login,phone ={}", param);
        return userService.login(param);
    }

    @GetMapping("/out")
    public Result out() {
        log.info("login out,id ={}", CurrentContext.getUserId());
        return userService.out(CurrentContext.get().getToken());
    }
}
